package cn.tedu.ssm.controller;

import cn.tedu.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService service;

    // 用于检查账号和密码，访问地址：http://localhost:8080/check
    @ResponseBody
    @RequestMapping("check")
    public Long check(String username, String password, HttpServletRequest request){
        Long id = service.checkUsernameAndPassword(username, password);
        // 从当前请求中获取回话信息（Session）
        HttpSession session = request.getSession();
        if (id != null && id > 0) {
            // 如果检查完账号和密码之后，能成功得到 ID，则将其保存到 Session 中
            session.setAttribute("UID", id);
        }
        return id;
    }

    @RequestMapping("login")
    public String loginPage() {
        return "login";
    }
}
